/**
 * File Name   : SQLMapClientFactory.java
 * Description : 
 */
package feng.process.dao;

import java.io.IOException;
import java.io.Reader;

import org.apache.log4j.Logger;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import feng.process.ProcessDataInfo;

/**
 * 程序直接加载SqlMapConfig.xml的方式构造SQLMapClient对象
 * @author 冯昌硕
 * @create 2012-10-25 上午12:36:30
 */
public class SQLMapClientFactory {

    private static final Logger log = Logger.getLogger(SQLMapClientFactory.class);

    private static SqlMapClient sqlMapper;

//    static {
//
//        log.debug("SqlMapClientFactory 构造 SqlMapClient");
//
//        if (sqlMapper == null) {
//            try {
//                Reader reader = Resources.getResourceAsReader("ProcessDBSqlMapConfig.xml");
//                sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
//                reader.close();
//            } catch (IOException e) {
//                log.error("加载SqlMapConfig.xml异常：" + e.toString());
//                e.printStackTrace();
//            }
//        }
//    }
    
    public static SqlMapClient getSqlMapClient() {
        if (sqlMapper == null) {
            log.debug("SqlMapClientFactory 构造 SqlMapClient");
            Reader reader = null;
            try {
                String sqlMapCfgFile = "ProcessDBSqlMapConfig_"+ ProcessDataInfo.dbType.toUpperCase() +".xml";
                log.debug("加载SqlMapConfig配置文件:[" + sqlMapCfgFile + "]");
                //reader = Resources.getResourceAsReader("ProcessDBSqlMapConfig.xml");
                reader = Resources.getResourceAsReader(sqlMapCfgFile);
                sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
                
            } catch (IOException e) {
                log.error("加载SqlMapConfig.xml异常：" + e.toString());
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("SqlMap reader关闭异常：" + e.toString());
                    e.printStackTrace();
                }
            }
        }
        return sqlMapper;
    }
}